<template>
	<view class="register">

		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput v-model="name" type="text" maxlength="11" placeholder="用户名"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="登录密码" isShowPass></wInput>
				<wInput v-model="email" type="text" placeholder="邮箱"></wInput>

			</view>

			<wButton class="wbutton" text="注 册" :rotate="isRotate" @click.native="startReg()"></wButton>

			<!-- 底部信息 -->
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon" :class="showAgree?'cuIcon-radiobox':'cuIcon-round'"> 同意</text>
				<!-- 协议地址 -->
				<navigator url="agreement" open-type="navigate">《用户协议》</navigator>和<navigator url="conceal" open-type="navigate">《隐私政策》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: "/static/logo.png",
				name: '', // 用户
				passData: '', //密码
				email: "", //验证码
				showAgree: false, //协议是否选择
				isRotate: false, //是否加载旋转
			}
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		methods: {
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},

			async startReg() {
				//注册
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请先同意《协议》'
					});
					return false;
				}
				if (this.name.length < 3) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名至少3位'
					});
					return false;
				}

				if (this.passData.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码至少6位'
					});
					return false;
				}
				if (this.email.length < 3 || !~this.email.indexOf('@')) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '邮箱不正确'
					});
					return false;
				}
				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)

				var options = {
					data: {
						s: "App.User.RegisterExt",
						password: this.passData,
						username: this.name,
						ext_info: "{\"yesapi_email\":\"" + this.email + "\",\"yesapi_nickname\":\"" + this.name + "\"}"
					},
					method: "POST"
				}
				const res = await this.$myRuquest(options)
				if (res.data.data.err_code == 0) {
					uni.showToast({
						title: '注册成功，请登录'
					});

					uni.reLaunch({
						url: "./login?backpage=my&backtype=2"
					});
				} else {
					uni.showToast({
						icon: 'none',
						title: "注册失败：" + res.data.data.err_msg,
					});
				}

			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
